package com.lenovo.smarttraffic.ui.activity.stage4;

import android.annotation.SuppressLint;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.SubwayInfoBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;

import java.util.List;

public class SubwayFindActivity extends BaseActivity implements ScaleGestureDetector.OnScaleGestureListener {

    private Toolbar toolbar;
    private TextView toolbar_title;

    private ExpandableListView listView;
    private ImageView image;

    private ScaleGestureDetector scaleGestureDetector;
    //矩阵对象
    private Matrix scaleMatrix = new Matrix();

    public static String title;

    @Override
    protected int getLayout() {
        return R.layout.activity_subway_find;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(v -> {
            if (image.getVisibility() == View.VISIBLE) {
                toggleView();
            } else finish();
        });
        toolbar_title = findViewById(R.id.toolbar_title);
        toolbar_title.setText("地铁查询");

        scaleGestureDetector = new ScaleGestureDetector(this, this);

        listView = findViewById(R.id.ex_listView);
        image = findViewById(R.id.image);
        image.setOnTouchListener((v, event) -> scaleGestureDetector.onTouchEvent(event));

        initData();
    }

    private void toggleView() {
        if (listView.getVisibility() == View.VISIBLE) {
            image.invalidate();
            listView.setVisibility(View.GONE);
            image.setVisibility(View.VISIBLE);
            toolbar_title.setText(title);
        } else {
            image.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            toolbar_title.setText("地铁查询");
        }
    }


    private void initData() {
        HttpUtils.post("get_metro", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> {
                    SubwayInfoBean subwayInfoBean = new Gson().fromJson(response.toString(), SubwayInfoBean.class);
                    List<SubwayInfoBean.ROWSDETAILBean> beanList = subwayInfoBean.getROWS_DETAIL();

                    listView.setAdapter(new SubwayInfoAdapter(beanList));
                    listView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
                        title = SubwayInfoAdapter.list.get(groupPosition).getStart_place() + "-" + SubwayInfoAdapter.list.get(groupPosition).getEnd_place();
                        toggleView();
                        return true;
                    });
                });
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float scaleFactor = detector.getScaleFactor();
        if (image == null) return true;
        scaleMatrix.postScale(scaleFactor, scaleFactor, image.getWidth() / 2, image.getHeight() / 2);
        image.setImageMatrix(scaleMatrix);
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }
}

class SubwayInfoAdapter extends BaseExpandableListAdapter {

    public static List<SubwayInfoBean.ROWSDETAILBean> list;

    public SubwayInfoAdapter(List<SubwayInfoBean.ROWSDETAILBean> list) {
        SubwayInfoAdapter.list = list;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
        return true;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_subway_group, parent, false);
            viewHolder = new GroupViewHolder();
            viewHolder.id_tv = convertView.findViewById(R.id.id_tv);
            viewHolder.point_tv = convertView.findViewById(R.id.point_tv);
            viewHolder.time_tv = convertView.findViewById(R.id.time_tv);
            viewHolder.expand = convertView.findViewById(R.id.expand);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }

        SubwayInfoBean.ROWSDETAILBean bean = list.get(groupPosition);

        viewHolder.id_tv.setText(bean.getMetro_code());
        viewHolder.point_tv.setText(bean.getStart_place() + "\n" + bean.getEnd_place());
        viewHolder.time_tv.setText("🕓" + bean.getStart_place_start_time() + "-" + bean.getStart_place_end_time() + "\n" +
                "🕓" + bean.getEnd_place_start_time() + "-" + bean.getEnd_place_end_time());

        //如果是展开状态，
        if (isExpanded) {
            viewHolder.expand.setRotation(270);
        } else {
            viewHolder.expand.setRotation(180);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_subway_child, parent, false);
            viewHolder = new ChildViewHolder();
            viewHolder.roads_tv = convertView.findViewById(R.id.roads_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }

        SubwayInfoBean.ROWSDETAILBean bean = list.get(groupPosition);

        viewHolder.roads_tv.setText(bean.getStart_place() + "\n" + bean.getStart_place() + "\n" +
                bean.getStart_place() + "\n" + bean.getStart_place() + "\n" + bean.getEnd_place());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //指定位置上的子元素是否可选中
        return true;
    }

    static class GroupViewHolder {
        TextView id_tv, point_tv, time_tv;
        View expand;
    }

    static class ChildViewHolder {
        TextView roads_tv;
    }
}
